import { Action, Declared } from "../types"
import { buildDeclared, buildDeclaredTree, getDeclares } from "./SeedUtil"

// /**
//  * 
//  * @param elements 合并方法上定义的元素
//  * @param column 通用列
//  * @returns 
//  */
// export function mergeActionElements(elements: Record<string, Column>, column: Column) {
//   if(!elements) return { ...column }

//   const actionColumn = elements[column.name] || {} // 'manager.name'
//   const items = column.name.split('.')
//   let tmp: Record<string, any> = elements
//   for(let i = 0; i < items.length - 1; i ++) { // manamger: {name: ''}
//     tmp = tmp[items[i]]
//     if(!tmp) break
//   }
//   if(tmp) return { ...column, ...actionColumn, ...(tmp[items[items.length - 1]] || {}) }

//   return { ...column, ...actionColumn }
// }

export function getDeclaresTree(action: Action) {console.log('getDeclaresTree===')
  const seed = action.seed
  const declares = getDeclares(action)
  const declared: Declared = buildDeclared(seed, declares)
  return buildDeclaredTree(seed, declared)
}

export function getIcon(action: Action) {
  const { icon, type } = action
  if(icon) return icon

  const dict = {
    page: 'ep:document-copy',
    list: 'ep:list',
    save: 'ep:circle-plus',
    saves: 'ep:circle-plus-filled',
    detail: 'ep:search',
    set: 'ep:setting',
    sets: 'ep:tools',
    remove: 'ep:remove',
    removes: 'ep:remove-filled',
    update: 'ep:edit',
    updates: 'ep:edit-pen',
    importExcel: 'mdi:table-import',
    exportExcel: 'mdi:table-export'
  }

  if(dict[type]) return dict[type]

  return 'grommet-icons:actions'
}

export function getLabel(action: Action) {
  const { label, type } = action
  if(label) return label

  const dict = {
    page: '分页列表',
    list: '列表',
    save: '新增',
    saves: '批量保存',
    detail: '详情',
    set: '设置',
    sets: '批量设置',
    remove: '删除',
    removes: '批量删除',
    update: '编辑',
    updates: '批量编辑',
    importExcel: '导入excel',
    exportExcel: '导出excel'
  }

  if(dict[type]) return dict[type]

  return '操作'
}

export function getUrl(action: Action) {
  console.log('getUrl===action', action)
  const { micro, seed } = action
  if(seed) return `${micro ? micro.name : '~'}/${seed.name}/${action.name}`

  return `${micro ? micro.name : '~'}/${action.name}`
}

// export function getActionName(action: Action) {
//   switch(action.type) {
//     case 'page':
//     case 'list':
//     case 'get':
//     case 'detail': return action.name
//     default: action.name
//   }
// }
